package com.jinyu.jyly.ui.mine.make_code;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jinyu.jyly.R;
import com.jinyu.jyly.base.BaseLazyFragment;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.bean.PayMentTypeInfo;
import com.jinyu.jyly.ui.mine.make_code.change.ChangeBankCodeActivity;
import com.jinyu.jyly.utils.AES;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 银行
 *
 * @author 62485
 */
public class MakeCodeBankTabFragment extends BaseLazyFragment<MakeCodePresenter> implements MakeCodeContract.View {


    @BindView(R.id.tv_bank_name)
    TextView tvBankName;
    @BindView(R.id.tv_bank_num)
    TextView tvBankNum;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_zhi_bank)
    TextView tvZhiBank;
    @BindView(R.id.btn_sub)
    Button btnSub;
    private MakeCodePresenter mPresenter;


    public static MakeCodeBankTabFragment newInstance(String type) {
        MakeCodeBankTabFragment newFragment = new MakeCodeBankTabFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        newFragment.setArguments(bundle);
        return newFragment;
    }

    @Override
    protected View initView(ViewGroup container) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_make_code_bank, container, false);
        return view;
    }

    @Override
    protected void loadData() {
        mTipDialog.show();
        mPresenter.getPayMent();
    }

    @Override
    public MakeCodePresenter getPresenter() {
        if (mPresenter == null) {
            mPresenter = new MakeCodePresenter(getActivity(), this);
        }
        return mPresenter;
    }


    @Override
    public void onFailer(Throwable throwable) {
        mTipDialog.dismiss();
    }

    @Override
    public void onPayMent(AllInfoBean infoBean) {
        mTipDialog.dismiss();
        switch (infoBean.getStatus()) {
            case 200:
                String beanData = infoBean.getData();
                String result = null;
                try {
                    result = AES.decrypt(beanData);
                    Gson gson = new Gson();
                    PayMentTypeInfo paymentInfo = gson.fromJson(result, PayMentTypeInfo.class);
                    if(paymentInfo!= null){
                        tvBankName.setText(paymentInfo.getBank());
                        tvBankNum.setText(paymentInfo.getBank_num());
                        tvName.setText(paymentInfo.getBank_name());
                        tvZhiBank.setText(paymentInfo.getBank_little_name());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(infoBean.getMsg());
                break;
            default:
                break;
        }
    }

    @OnClick(R.id.btn_sub)
    public void onViewClicked() {
        Intent mIntent = new Intent(getActivity(), ChangeBankCodeActivity.class);
        startActivity(mIntent);
    }
}
